home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / roids / lines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  2.6 KB  |  84 lines

  1. /*
  2.  * Copyright 1989 Digital Equipment Corporation
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted,
  6.  * provided that the above copyright notice appear in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation, and that the name of Digital Equipment
  9.  * Corporation not be used in advertising or publicity pertaining to
  10.  * distribution of the software without specific, written prior
  11.  * permission.  Digital Equipment Corporation makes no representations
  12.  * about the suitability of this software for any purpose.  It is
  13.  * provided "as is" without express or implied warranty.
  14.  *
  15.  * DIGITAL EQUIPMENT CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16.  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR
  18.  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20.  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Terry Weissman
  24.  *          weissman@decwrl.dec.com
  25.  */
  26.  
  27. /* lines.c -- paint a bunch of lines with minimal requests. */
  28.  
  29. /* %%% Too lazy to compress these; maybe I'll do it later. */
  30.  
  31.  
  32. #include "roids.h"
  33.  
  34. void BeginLines() {}
  35.  
  36. void AddLine(fromx, fromy, tox, toy, gc)
  37. int fromx, fromy, tox, toy;
  38. GC gc;
  39. {
  40.     XDrawLine(dpy, gamewindow, gc, fromx, fromy, tox, toy);
  41. }
  42.  
  43. void AddRec(fromx, fromy, gc)
  44. int fromx, fromy;
  45. GC gc;
  46. {
  47.     XDrawRectangle(dpy, gamewindow, gc, fromx, fromy+1, 1,1);
  48. }
  49.  
  50. void EndLines() {}
  51.  
  52.  
  53. #define BETWEEN(x, a, b) ((a <= b && x >= a && x <= b) ||    \
  54.               (a > b && x <= a && x >= b))
  55.  
  56. Boolean CheckIntersects(ax, ay, bx, by, cx, cy, dx, dy)
  57. int ax, ay, bx, by, cx, cy, dx, dy;
  58. {
  59.     float m1, m2, b1, b2, x, y;
  60.     if (bx == ax) {
  61.     if (dx == cx) return FALSE; /* Parallel; hacking, assume they miss. */
  62.     m2 = ((float) (dy - cy)) / ((float) (dx - cx));
  63.     b2 = cy - cx*m2;
  64.     x = ax;
  65.     y = m2 * x + b2;
  66.     return (BETWEEN(y, ay, by) && BETWEEN(x, cx, dx) &&
  67.         BETWEEN(y, cy, dy));
  68.     }
  69.     if (dx == cx) {
  70.     return CheckIntersects(cx, cy, dx, dy, ax, ay, bx, by);
  71.     }
  72.     m1 = ((float) (by - ay)) / ((float) (bx - ax));
  73.     m2 = ((float) (dy - cy)) / ((float) (dx - cx));
  74.     b1 = ay - ax*m1;
  75.     b2 = cy - cx*m2;
  76.     if (m1 == m2) {        /* Parallel lines */
  77.     return FALSE;        /* Hack. */
  78.     }
  79.     x = (b2 - b1) / (m1 - m2);
  80.     y = m1 * x + b1;
  81.     return (BETWEEN(x, ax, bx) && BETWEEN(y, ay, by) &&
  82.         BETWEEN(x, cx, dx) && BETWEEN(y, cy, dy));
  83. }
  84.